package com.nageoffer.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.project.common.result.Result;
import com.nageoffer.shortlink.project.common.result.Results;
import com.nageoffer.shortlink.project.dto.req.ShortLinkCreateReqDto;
import com.nageoffer.shortlink.project.dto.req.ShortLinkPageReqDto;
import com.nageoffer.shortlink.project.dto.req.ShortLinkUpdateReqDto;
import com.nageoffer.shortlink.project.dto.resp.ShortLinkCountQueryRespDto;
import com.nageoffer.shortlink.project.dto.resp.ShortLinkCreateRespDto;
import com.nageoffer.shortlink.project.dto.resp.ShortLinkPageRespDto;
import com.nageoffer.shortlink.project.service.ShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortLinkService;

    /**
     *短链接跳转
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) throws IOException {
        shortLinkService.restoreUrl(shortUri,request,response);
    }
    /**
     *创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDto> createShortLink(@RequestBody ShortLinkCreateReqDto requestParam){
        return Results.success(shortLinkService.createShortLink(requestParam));
    }

    /**
     *短链接分页查询
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDto>> pageShortLink(ShortLinkPageReqDto requestParam){
        return Results.success(shortLinkService.pageShortLink(requestParam));
    }

    /**
     * 查询短链接分组数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkCountQueryRespDto>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam){
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
    }

    /**
     * 修改短链接
     */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDto requestParam){
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }
}
